/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gp.vlc.rp.net;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;

/**
 *
 * @author Namo
 */
public class MyFactory {

    public static DefaultHttpClient createHttpClient(ReverseProxyConfiguration config) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException {
        System.setProperty("jsse.enableSNIExtension", "false");
        DefaultHttpClient defaultHttpClient;
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory mySSLSocketFactory = new MySSLSocketFactory(trustStore);
        mySSLSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", mySSLSocketFactory, 443));
        ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);

        defaultHttpClient = new DefaultHttpClient(ccm);

        if (!config.getProxyHost().isEmpty()) {
            HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort(), "http");
            defaultHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }

        // set credentials for server
        int port = config.getURI().getPort();

        if (-1 == port) {
            if (config.getURI().getAuthority().equalsIgnoreCase("https")) {
                port = 443;
            }
            else if (config.getURI().getAuthority().equalsIgnoreCase("http")) {
                port = 80;
            }
        }

        defaultHttpClient.getCredentialsProvider().setCredentials(new AuthScope(config.getURI().getHost(), port), config.getCredentials());

        // set credentials for proxy
        defaultHttpClient.getCredentialsProvider().setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), config.getProxyCredentials());
        return defaultHttpClient;
    }
}
